.include "asm/include/battle_commands.inc"

.data

_000:
    Call BATTLE_SUBSCRIPT_PUSH_ATTACKER_AND_DEFENDER

_loop:
    TryPursuit _end
    TryMegaOrUltraBurstDuringPursuit _noMega
    CallFromVar BSCRIPT_VAR_TEMP_DATA
    // Handle cases such as Mega Manectric's Intimidate ability (Switch -> Mega Evolution -> Intimidate -> Pursuit)
    // 處理例如超級雷電獸的威嚇特性 （切換中 -> 超級進化 -> 威嚇 -> 追打）
    Call BATTLE_SUBSCRIPT_SWITCH_IN_ABILITY_CHECK
_noMega:
    // double the power for damage calc
    UpdateVar OPCODE_SET, BSCRIPT_VAR_POWER_MULTI, 20
    CalcCrit 
    CalcDamage 
    ApplyTypeEffectiveness 
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_NO_ATTACK_MESSAGE
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_MOVE_ANIMATIONS_OFF
    PrintAttackMessage 
    Wait 
    CompareVarToValue OPCODE_FLAG_SET, BSCRIPT_VAR_MOVE_STATUS_FLAGS, MOVE_STATUS_DID_NOT_HIT, _moveMissed
    PlayMoveAnimation BATTLER_CATEGORY_ATTACKER
    Wait 
    CheckSubstitute BATTLER_CATEGORY_DEFENDER, _hasSubstitute
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_TEMP_DATA, BSCRIPT_VAR_DAMAGE
    UpdateVar OPCODE_MUL, BSCRIPT_VAR_TEMP_DATA, -1
    CompareMonDataToVar OPCODE_LTE, BATTLER_CATEGORY_DEFENDER, BMON_DATA_HP, BSCRIPT_VAR_TEMP_DATA, _willFaintUpdateShellBell
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, BSCRIPT_VAR_DAMAGE
    GoTo _noNeedToUpdateShellBell

_willFaintUpdateShellBell:
    UpdateMonDataFromVar OPCODE_GET, BATTLER_CATEGORY_DEFENDER, BMON_DATA_HP, BSCRIPT_VAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT
    UpdateVar OPCODE_MUL, BSCRIPT_VAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, -1

_noNeedToUpdateShellBell:
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_HP_CALC, BSCRIPT_VAR_DAMAGE
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_PHYSICAL_DAMAGE_TAKEN, BSCRIPT_VAR_DAMAGE
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_MSG_BATTLER_TEMP, BSCRIPT_VAR_BATTLER_TARGET
    CheckHoldOnWith1HP BATTLER_CATEGORY_MSG_TEMP
    Call BATTLE_SUBSCRIPT_UPDATE_HP
    Call BATTLE_SUBSCRIPT_CRITICAL_HIT
    Call BATTLE_SUBSCRIPT_MOVE_FOLLOWUP_MESSAGE
    CompareMonDataToValue OPCODE_EQU, BATTLER_CATEGORY_DEFENDER, BMON_DATA_HP, 0, _defenderIsFainting
    TriggerAbilityOnHit _090
    CallFromVar BSCRIPT_VAR_TEMP_DATA

_090:
    TriggerHeldItemOnHit _094
    CallFromVar BSCRIPT_VAR_TEMP_DATA

_094:
    UpdateVar OPCODE_FLAG_ON, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_MOVE_SUCCESSFUL
    TriggerHeldItemOnPivotMove _102
    CallFromVar BSCRIPT_VAR_TEMP_DATA

_102:
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_MOVE_SUCCESSFUL
    GoTo _resetLoop

_hasSubstitute:
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_TEMP_DATA, BSCRIPT_VAR_DAMAGE
    UpdateVar OPCODE_MUL, BSCRIPT_VAR_TEMP_DATA, -1
    CompareMonDataToVar OPCODE_LTE, BATTLER_CATEGORY_DEFENDER, BMON_DATA_SUBSTITUTE_HP, BSCRIPT_VAR_TEMP_DATA, _subWillBreak
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, BSCRIPT_VAR_DAMAGE
    GoTo _hitSubNoBreak

_subWillBreak:
    UpdateMonData OPCODE_FLAG_OFF, BATTLER_CATEGORY_DEFENDER, BMON_DATA_STATUS2, STATUS2_SUBSTITUTE
    UpdateMonDataFromVar OPCODE_GET, BATTLER_CATEGORY_DEFENDER, BMON_DATA_SUBSTITUTE_HP, BSCRIPT_VAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT
    UpdateVar OPCODE_MUL, BSCRIPT_VAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, -1

_hitSubNoBreak:
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_MSG_BATTLER_TEMP, BSCRIPT_VAR_BATTLER_TARGET
    Call BATTLE_SUBSCRIPT_HIT_SUBSTITUTE
    Call BATTLE_SUBSCRIPT_CRITICAL_HIT
    Call BATTLE_SUBSCRIPT_MOVE_FOLLOWUP_MESSAGE
    GoTo _resetLoop

_moveMissed:
    WaitButtonABTime 15
    Call BATTLE_SUBSCRIPT_MISSED

_resetLoop:
    Call BATTLE_SUBSCRIPT_POP_ATTACKER_AND_DEFENDER
    UpdateVarFromVar OPCODE_GET, BSCRIPT_VAR_MOVE_NO_TEMP, BSCRIPT_VAR_MOVE_NO_CUR
    GoTo _loop


// rest of the file is only done if the defender is fainting after getting hit


_defenderIsFainting:
    Call BATTLE_SUBSCRIPT_FAINT_CHECK_DESTINY_BOND
    TriggerAbilityOnHit _172
    CallFromVar BSCRIPT_VAR_TEMP_DATA

_172:
    TriggerHeldItemOnHit _176
    CallFromVar BSCRIPT_VAR_TEMP_DATA

_176:
    UpdateVar OPCODE_FLAG_ON, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_MOVE_SUCCESSFUL
    TriggerHeldItemOnPivotMove _184
    CallFromVar BSCRIPT_VAR_TEMP_DATA

_184:
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_MOVE_SUCCESSFUL
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_TEMP_DATA, BSCRIPT_VAR_BATTLER_FAINTED
    UpdateVar OPCODE_SET, BSCRIPT_VAR_BATTLER_FAINTED, BATTLER_PLAYER
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_CALC_TEMP, BSCRIPT_VAR_BATTLE_STATUS_2
    UpdateVar OPCODE_RIGHT_SHIFT, BSCRIPT_VAR_CALC_TEMP, 0x0000001C
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS_2, BATTLE_STATUS2_EXP_GAIN

_expLoop:
    // only give experience to the player's mons
    CompareVarToValue OPCODE_FLAG_NOT, BSCRIPT_VAR_CALC_TEMP, 0x00000001, _215
    Call BATTLE_SUBSCRIPT_GRANT_EXP

_215:
    UpdateVar OPCODE_ADD, BSCRIPT_VAR_BATTLER_FAINTED, BATTLER_ENEMY
    UpdateVar OPCODE_RIGHT_SHIFT, BSCRIPT_VAR_CALC_TEMP, 0x00000001
    CompareVarToValue OPCODE_NEQ, BSCRIPT_VAR_CALC_TEMP, 0x00000000, _expLoop
    UpdateVarFromVar OPCODE_SET, BSCRIPT_VAR_BATTLER_FAINTED, BSCRIPT_VAR_TEMP_DATA
    Call BATTLE_SUBSCRIPT_POP_ATTACKER_AND_DEFENDER
    UpdateVarFromVar OPCODE_GET, BSCRIPT_VAR_MOVE_NO_TEMP, BSCRIPT_VAR_MOVE_NO_CUR

_end:
    // fix bug where pursuit on the rightside client was preventing the next attack message/move animation from playing
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_NO_ATTACK_MESSAGE
    UpdateVar OPCODE_FLAG_OFF, BSCRIPT_VAR_BATTLE_STATUS, BATTLE_STATUS_MOVE_ANIMATIONS_OFF
    End 
